#include <jni.h>
#include <string>
#include "MyLog.h"
/**
 *    // 1. JNIEnv：代表了VM里面的环境，本地的代码可以通过该参数与Java代码进行操作
       // 2. obj：定义JNI方法的类的一个本地引用（this）
 */
extern "C"
JNIEXPORT jstring JNICALL
Java_com_caldremch_caldremcore_CalCore_getStringFromJni(JNIEnv *env, jobject instance) {

    std::string hello = "Hello from C++ from core";
    log_info("File Name : %s", "Hello world from mylog open CCORERELEASE");
    return env->NewStringUTF(hello.c_str());

}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_caldremch_caldremcore_CalCore_localRef(JNIEnv *env, jclass type) {

    int i = 0;
    for (; i < 10; i++) {

        //在循环中创建了占用内存大的对象
        jclass clz_date = env->FindClass("java/util/Date");
        jmethodID mid = env->GetMethodID(clz_date, "<init>", "()V");
        jobject jobject_date = env->NewObject(clz_date, mid);
        //....
        //不再使用jobject对象
        //通知垃圾回收器回收对象, 确保内存充足
        env->DeleteLocalRef(jobject_date);
    }

}
//全局引用的字符串对象
jstring global_jstr;
extern "C"
JNIEXPORT void JNICALL
Java_com_caldremch_caldremcore_CalCore_createGlobalRef(JNIEnv *env, jobject instance) {
    global_jstr = env->NewStringUTF("I LOVE YOU");
    env->NewGlobalRef(global_jstr);
}


extern "C"
JNIEXPORT jstring JNICALL
Java_com_caldremch_caldremcore_CalCore_getGlobalRef(JNIEnv *env, jobject instance) {
    return global_jstr;
}extern "C"
JNIEXPORT void JNICALL
Java_com_caldremch_caldremcore_CalCore_deteleGlobalRef(JNIEnv *env, jobject instance) {

    env->DeleteGlobalRef(global_jstr);
}extern "C"
JNIEXPORT void JNICALL
Java_com_caldremch_caldremcore_CalCore_testException(JNIEnv *env, jobject instance) {

    jclass clz = env->GetObjectClass(instance);
    //属性名不小心拿错了, 拿到的事空的fildeid
    jfieldID  fid = env->GetFieldID(clz, "key1", "Ljava/lang/String;");

    jthrowable  err = env->ExceptionOccurred();

    if(err != NULL){
        //手动清空异常信息, 保证java代码能够继续进行
        env->ExceptionClear();
        //提供补救措施, 例如获取另外一个属性
        fid = env->GetFieldID(clz, "key", "Ljava/lang/String;");
    }

    //也可以自己抛出异常, 可以在java中捕获
    if (1 == 2){//异常条件 临界
        //抛出IllegalArgumentException,
        jclass err_clz = env->FindClass("java/lang/IllegalArgumentException");
        env->ThrowNew(err_clz, "key values is invalid!");
    }

}extern "C"
JNIEXPORT void JNICALL
Java_com_caldremch_caldremcore_CalCore_testcache(JNIEnv *env, jobject instance) {

    static jfieldID fid = NULL;
    jclass  clz = env->GetObjectClass(instance);
    if(fid == NULL){
        fid = env->GetFieldID(clz, "key", "Ljava/lang/String;");
        printf("fid init once");
    }


}extern "C"
JNIEXPORT void JNICALL
Java_com_caldremch_caldremcore_CalCore_variable_1parameter_1method(JNIEnv *env, jobject instance,


                                                                   jobjectArray var) {}

